import { defineStore } from 'pinia';
import {
    getUserList,
    createUser,
    updateUser,
    deleteUser,
    getUserDetail,
} from '@/api/user/user.js';
import { ElMessage } from "element-plus";

export const useUserManageStore = defineStore('userManage', {
    state: () => ({
        userList: [],
        total: 0,
        multipleSelection: [],
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            username: '',
            name: '',
            role: '',
        },
        form: {
            id: null,  // 添加 id 字段
            username: '',
            password: '',
            name: '',
            phone: '',
            email: '',
            role: '',
        },
        dialogVisible: false,
        dialogTitle: '',
    }),
    getters: {},
    actions: {
        async getList() {
            try {
                const res = await getUserList(this.queryParams);
                this.userList = res.data.records;
                this.total = parseInt(res.data.total);
            } catch (error) {
                console.error('获取用户列表失败', error);
                ElMessage.error('获取用户列表失败'); // 添加错误提示
            }
        },
        setPageSize(size) {
            this.queryParams.pageSize = size;
        },
        setPageNum(page) {
            this.queryParams.pageNum = page;
        },
        resetQuery() {
            this.queryParams.username = '';
            this.queryParams.name = '';
            this.queryParams.role = '';
            this.queryParams.pageNum = 1;
        },
        openCreateDialog() {
            Object.assign(this.form, {
                id: null,
                username: '',
                password: '',
                name: '',
                phone: '',
                email: '',
                role: '',
            });
            this.dialogTitle = '新增用户';
            this.dialogVisible = true;
        },
        openEditDialog(row) {
            getUserDetail(row.id)
                .then(res => {
                    this.form = {
                        id: res.data.id,
                        username: res.data.username,
                        password: res.data.password,
                        name: res.data.name,
                        phone: res.data.phone,
                        email: res.data.email,
                        role: res.data.role,
                    };
                    this.dialogTitle = '编辑用户';
                    this.dialogVisible = true;
                })
                .catch(error => {
                    ElMessage.error('获取用户详情失败');
                    console.error(error);
                });
        },

        async submitForm() {
            try {
                if (this.form.id) {
                    await updateUser(this.form.id, this.form);
                    ElMessage.success('更新成功');
                } else {
                    await createUser(this.form);
                    ElMessage.success('新增成功');
                }
                this.dialogVisible = false;
                await this.getList(); // 确保更新数据列表
            } catch (error) {
                console.error('操作失败', error);
                ElMessage.error('操作失败');
                return Promise.reject(error)
            }
        },
        async handleDelete(row) {
            try {
                await deleteUser([row.id]);
                ElMessage.success('删除成功');
                await this.getList(); // 确保更新数据列表
            } catch (error) {
                console.error('删除失败', error);
                ElMessage.error('删除失败');
            }
        },
        setMultipleSelection(selection) {
            this.multipleSelection = selection;
        },
        async handleBatchDelete() {
            try {
                const ids = this.multipleSelection.map((item) => item.id);
                await deleteUser(ids);
                ElMessage.success('批量删除成功');
                await this.getList(); // 确保更新数据列表
                this.multipleSelection = []; // 清空选中项
            } catch (error) {
                console.error('批量删除失败', error);
                ElMessage.error('批量删除失败');
            }
        },
    },
});